# Remap prefix key to C-a, which is GNU-Screen compatible.
unbind C-b
set-option -g prefix C-a
bind C-a send-prefix

# Enable xterm-style function key sequences.
set-option -gw xterm-keys on

# Enable mouse mode.
set-option -g mouse on

# Faster command sequences.
set-option -s escape-time 10

# Slower repeat timeout, unit: milliseconds.
set-option -gs repeat-time 600

# Enable focus event.
set-option -s focus-events on

# Boost history.
set-option -g history-limit 5000

# Use UTF-8.
set-option -gqw utf8 on

# Start windows and panes numbering at 1.
set-option -g base-index 1
set-option -gw pane-base-index 1

# Rename window to reflect current program.
set-option -gw automatic-rename on

# Renumber windows when a window is closed.
set-option -g renumber-windows on

# Set terminal title.
set-option -g set-titles on

## Status bar style.
# redraw status line per minute.
set-option -g status-interval 60 # unit, seconds
set-option -g status-style fg=white,bg=black
set-option -g status-left '#[fg=white,bg=magenta] #S '
set-option -g status-right '#{?client_prefix,#[reverse] ^A #[noreverse],} #[fg=white,bg=black]%a %R %p %Y-%m-%d #{USER} #h'

# Position of the window list.
set-option -g status-justify left
# set-option -g status-justify centre

# Set window status style.
set-option -g window-status-separator ''

# Default window style.
# Use `default` to inherit the color of the status bar.
set-option -g window-status-format ' #I:#W#F '
set-option -g window-status-style fg=default,bg=default

# Current window style.
set-option -g window-status-current-format ' #I:#W#F '
set-option -g window-status-current-style fg=black,bg=yellow,bright

# Last active window.
set-option -g window-status-last-style fg=white,bg=default,underscore

# Window with an activity alert.
set-option -g window-status-activity-style fg=cyan,bg=default

# Window with an bell alert.
set-option -g window-status-bell-style fg=red,bg=default

# Set pane border style.
set-option -g pane-border-style fg=colour240,bg=default
set-option -g pane-active-border-style fg=green,bg=default

# # Set the Command Line.
# set-option -g message-style fg=default,bg=yellow

# Clock.
set-option -gw clock-mode-colour green

# -- Key Bindings

# Global:
#   prefix r        Reload tmux configuration file.
#   prefix t        Show the time.
#   prefix ?        List all key bindings.
#   prefix :        Enter the tmux command prompt.
#   ctrl+l          Clear both screen and history.

# Reload configuration.
bind r source-file ~/.tmux.conf \; display-message 'tmux configuration reloaded.'

# Clear both screen and history.
bind -n C-l send-keys C-l \; run 'sleep 0.1' \; clear-history

# Session:
#   prefix C-c        Create a new session.
#   prefix s          Choose the current session interactively.
#   prefix $          Rename the current session.
#   prefix d          Detach the current client.
#   prefix )          Change to the next session.
#   prefix (          Change to the previous session.

# Create session.
bind C-c new-session

# Find session.
bind C-f command-prompt -p find-session 'switch-client -t %%'

# Change session.
bind -r ( switch-client -p
bind -r ) switch-client -n

# Window:
#   prefix c        Create a new window.
#   prefix w        Choose the current window interactively.
#   prefix C-h      Change to the next window.
#   prefix C-l      Change to the previous window.
#   prefix f        Find opened windows.
#   prefix ,        Rename the current window.
#   prefix &        Kill the current window.
#   prefix C

# Window navigation.
unbind n
unbind p
# Add -r to make the key repeatable
bind -r C-h previous-window     # select previous window
bind -r C-l next-window         # select next window
bind Tab last-window            # move to last active window

# Pane:
#   prefix |        Split the current pane into two, left and right.
#   prefix -        Split the current pane into two, top and bottom.
#   prefix o        Select the next pane in the current window.
#   prefix q        Briefly display pane indexes.
#   prefix x        Kill the current pane.
#   prefix space    Arrange the current window in the next preset layout.
#   prefix <        Swap the current pane with the previous pane.
#   prefix >        Swap the current pane with the next pane.
#   prefix z        Toggle zoom state of the current pane.

## the same behavior as Emacs.
# On macOS, new panes will be opened under where the tmux session started.
# Split panes using | and -.
bind | split-window -h
bind - split-window -v
unbind '"'
unbind %

# Pane navigation.
bind -r h select-pane -L     # move left
bind -r j select-pane -D     # move down
bind -r k select-pane -U     # move up
bind -r l select-pane -R     # move right
bind > swap-pane -D          # swap current pane with the next one
bind < swap-pane -U          # swap current pane with the previous one

# Pane resizing.
bind -r H resize-pane -L 2
bind -r J resize-pane -D 2
bind -r K resize-pane -U 2
bind -r L resize-pane -R 2

# Check information about processes.
bind T new-window -n processes "top"

# Maximizing and restoring panes.
bind O new-window -d -n tmp \; swap-pane -s tmp.1 \; select-window -t tmp
bind I last-window \; swap-pane -s tmp.1 \; kill-window -t tmp

bind b list-buffers  # list paste buffers
bind p paste-buffer  # paste from the top paste buffer
bind P choose-buffer # choose which buffer to paste from
bind L pipe-pane -o "cat >> /tmp/buffer.log" \; display-message "Toggled log to /tmp/buffer.log"

# Use Emacs keybindings in the status line.
set-option -g status-keys emacs

# Enable Vi mode.
set-option -g mode-keys vi

# Colors

# # How to use true colors in vim under tmux? #1246
# # https://github.com/tmux/tmux/issues/1246:
#
# # Put this in your ~/.tmux.conf and replace XXX by your $TERM outside of tmux:
# # $TERM should be "screen-256color" or "tmux-256color" in tmux.
#
# # Solve VIM's problem but lead to Emacs's problem since Emacs can't recognize tmux-256color.
# # Try screen-256color (https://github.com/tmux/tmux/issues/622):
#
# # for 2.6 and higher
# if-shell "test '\( #{$TMUX_VERSION_MAJOR} -eq 2 -a #{$TMUX_VERSION_MINOR} -ge 6 \)'" 'set -g default-terminal "screen-256color"'
# if-shell "test '\( #{$TMUX_VERSION_MAJOR} -eq 2 -a #{$TMUX_VERSION_MINOR} -ge 6 \)'" 'set -sa terminal-overrides ",*256col*:RGB"'
#
# # for 2.5 and lower
# if-shell '\( #{$TMUX_VERSION_MAJOR} -eq 2 -a #{$TMUX_VERSION_MINOR} -lt 6\) -o #{$TMUX_VERSION_MAJOR} -le 1' 'set -g default-terminal "screen-256color"'
# if-shell '\( #{$TMUX_VERSION_MAJOR} -eq 2 -a #{$TMUX_VERSION_MINOR} -lt 6\) -o #{$TMUX_VERSION_MAJOR} -le 1' 'set -ga terminal-overrides ",screen-256color:Tc"'

# # Enable Neovim cursor shapes in tmux.
# # https://github.com/neovim/neovim/wiki/FAQ#cursor-shape-doesnt-change-in-tmux
# set-option -sa terminal-overrides '*:Ss=\E[%p1%d q:Se=\E[2 q'

# Copy Mode:
#   prefix [        Enter copy mode to copy text or view the history.
#   prefix ]        Paste the most recently copied buffer of text.
#   space           Start selection.
#   esc             Clear selection.
#   enter           Copy selection.
#   q               Quit copy mode.

# Enter copy mode.
bind Enter copy-mode

run -b 'tmux bind -t vi-copy v begin-selection 2> /dev/null || true'
run -b 'tmux bind -T copy-mode-vi v send -X begin-selection 2> /dev/null || true'
run -b 'tmux bind -t vi-copy C-v rectangle-toggle 2> /dev/null || true'
run -b 'tmux bind -T copy-mode-vi C-v send -X rectangle-toggle 2> /dev/null || true'
run -b 'tmux bind -t vi-copy y copy-selection 2> /dev/null || true'
run -b 'tmux bind -T copy-mode-vi y send -X copy-selection-and-cancel 2> /dev/null || true'
run -b 'tmux bind -t vi-copy Escape cancel 2> /dev/null || true'
run -b 'tmux bind -T copy-mode-vi Escape send -X cancel 2> /dev/null || true'
run -b 'tmux bind -t vi-copy H start-of-line 2> /dev/null || true'
run -b 'tmux bind -T copy-mode-vi H send -X start-of-line 2> /dev/null || true'
run -b 'tmux bind -t vi-copy L end-of-line 2> /dev/null || true'
run -b 'tmux bind -T copy-mode-vi L send -X end-of-line 2> /dev/null || true'

# Copy to macOS clipboard.
if -b 'command -v pbcopy > /dev/null 2>&1' 'bind y run -b "tmux save-buffer - | pbcopy"'
if -b 'command -v reattach-to-user-namespace > /dev/null 2>&1' 'bind y run -b "tmux save-buffer - | reattach-to-user-namespace pbcopy"'
# Paste from macOS clipboard.
if -b 'command -v pbpaste > /dev/null 2>&1' 'bind C-v run -b "tmux set-buffer \"$(pbcopy)\"; tmux paste-buffer"'
if -b 'command -v reattach-to-user-namespace > /dev/null 2>&1' 'bind C-v run -b "tmux set-buffer \"$(reattach-to-user-namespace pbpaste)\"; tmux paste-buffer"'

# Copy to X11 clipboard.
if -b 'command -v xsel > /dev/null 2>&1' 'bind y run -b "tmux save-buffer - | xsel -i -b"'
if -b '! command -v xsel > /dev/null 2>&1 && command -v xclip > /dev/null 2>&1' 'bind y run -b "tmux save-buffer - | xclip -i -selection clipboard >/dev/null 2>&1"'
# Paste from X11 clipboard.
if -b 'command -v xsel > /dev/null 2>&1' 'bind C-v run -b "tmux set-buffer \"$(xsel -o -b clipboard)\"; tmux paste-buffer"'
if -b '! command -v xsel > /dev/null 2>&1 && command -v xclip > /dev/null 2>&1' 'bind C-v run -b "tmux set-buffer \"$(xclip -o -selection clipboard >/dev/null 2>&1)\"; tmux paste-buffer"'

# Copy to Windows clipboard.
if -b 'command -v clip.exe > /dev/null 2>&1' 'bind y run -b "tmux save-buffer - | clip.exe"'
if -b '[ -c /dev/clipboard ]' 'bind y run -b "tmux save-buffer - > /dev/clipboard"'
# Paste from Windows clipboard.
if -b 'command -v clip.exe > /dev/null 2>&1' 'bind C-v run -b "tmux set-buffer clip.exe; tmux paste-buffer"'
if -b '[ -c /dev/clipboard ]' 'bind C-v run -b "tmux set-buffer /dev/clipboard; tmux paste-buffer"'
